import { BasicMarker } from '../../js/BasicMarker.js';
import { BasicMarkers } from '../../js/BasicMarkers.js';
import { getMarkerIcon } from '../../js/icon.js';
import { getDevLocation } from '@gis/lib/api';
// import { openDeviceDialog } from '@/views/equipment-manage/components/overview-info/overview-info-dialog';
import { useUserInfoStore } from '@gis/lib/pinia/userInfo.js';
import { handleCommonRes } from '@gis/lib/utils/resolver.js';
import GzsbWindow from '../../info-window/Gzsb.js';

// const typeCodeEnum = {
//   1: "ptjk",
//   D0112_1: "ptjk",
//   2: "yyjk",
//   3: "rljk",
//   4: "ydjk",
//   "7_01": "ygbj",
//   "7_02": "dqjc",
//   "7_03": "szjc",
//   ptjk: "ptjk",
//   yyjk: "yyjk",
//   rljk: "rljk",
//   ydjk: "ydjk",
//   mj: "mj",
//   dz: "dz",
//   znsh: "znsh",
//   qt: "ptjk",
// };

class Marker extends BasicMarker {
  constructor({ map, data }) {
    super({ map, data });
    /* this.type: 这个type主要用于筛选,需要和filter-legend里面的type相对应 */
    // this.type = typeCodeEnum[data.type_code];
    this.type = data.type;
    super.create();
  }
  onClick(e) {
    new GzsbWindow({ map: this.map }).open(e.target);
  }
}

export default class Gzsb extends BasicMarkers {
  constructor({ map }) {
    super();
    this.map = map;
  }
  createMarker(data) {
    return new Marker({ map: this.map, data });
  }
  async getDataAsync() {
    const { area_code } = useUserInfoStore();
    const data = await handleCommonRes(getDevLocation, {
      area_code: area_code,
    });
    // const xlgc_obj = data.xlgc_type || {};
    // data.rlzp = xlgc_obj['D0112_4'] || [];
    // delete xlgc_obj['D0112_4'];
    // data.xlgc = Object.keys(xlgc_obj).reduce((res, key) => res.concat(xlgc_obj[key]), []);

    /* xlgc_type 这个字段返回了雪亮工程中所有类型，跟 xlgc 字段中的数据一致，只是格式不一样，目前暂时用不着 */
    delete data.xlgc_type;

    // console.log(data);
    return Object.keys(data).reduce((res, key) => {
      let list = Array.isArray(data[key]) ? data[key] : [data[key]];
      list = list.reduce((res, item) => {
        if (!item || !Number(item.longitude) || !Number(item.latitude)) {
          return res;
        }
        res.push(
          Object.assign(item, {
            type: key,
            type_code: item.type || key,
            icon: getMarkerIcon(key),
            lnglat: [item.longitude, item.latitude],
          })
        );
        return res;
      }, []);
      // console.log('Gzsb data: ', key, list);
      return [].concat(res, list);
    }, []);
  }
}
